package com.hzit.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.pojo.*;
import com.hzit.service.ClassesService;
import com.hzit.service.StudentService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	private ClassesService classesService;

	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		studentService = wac.getBean(StudentService.class);
		classesService = wac.getBean(ClassesService.class);

	}

	//2. 通过请求参数调用不同的方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf8");
		//2.1 得到请求参数
		String cmd = req.getParameter("cmd");
		//2.2 通过参数调用不同方法
		if (StrUtil.isNotBlank(cmd)) {
			if ("list".equals(cmd)) {
				list(req, resp);
			}
			if ("search".equals(cmd)) {
				search(req, resp);
			}
			if ("add".equals(cmd)) {
				add(req, resp);
			}
			if ("update".equals(cmd)) {
				update(req, resp);
			}
			if ("delete".equals(cmd)) {
				delete(req, resp);
			}
		}
	}
	//3. 学生列表
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//3.1 得到所有学生列表
		List<Student> studentList = studentService.findAll();
		//3.2 将学生列表放到R对象转换为字符串
		R r = R.ok().data("students", studentList);
		String s = JSON.toJSONString(r);

		//3.2 将字符串输出到页面
		resp.getWriter().println(s);

		resp.getWriter().flush();
	}

	//4. 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//4.1 得到并处理分页参数
		int page = 1;
		int pageSize = 3;
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		if (StrUtil.isNotBlank(pageStr)) {
			page = new Integer(pageStr);
		}
		if (StrUtil.isNotBlank(pageSizeStr)) {
			pageSize = new Integer(pageSizeStr);
		}
		//4.2 得到查询条件对象
		String studStr = req.getParameter("stud");
		StudentVo vo = JSON.parseObject(studStr, StudentVo.class);
		//4.3 开始查询
		PageResult<Student> pr = studentService.search(page, pageSize, vo);
		R r = R.ok().data("pr", pr);

		//4.4 进行页面跳转
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();
	}

	//5.添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//5.1 得到要添加的学生信息
		String stud = req.getParameter("stud");
		Student student = JSON.parseObject(stud, Student.class);
		//5.2 添加学生
		studentService.insert(student);
		//5.3 构造R对象
		R r = R.ok().message("添加学生成功！");
		//5.4 转换为json并输出到前端
		String s = JSON.toJSONString(r);
		resp.getWriter().println(s);
		resp.getWriter().flush();

	}

	//6. 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		//6.1 得到要添加的学生信息
		String stud = req.getParameter("stud");
		Student student = JSON.parseObject(stud, Student.class);
		//6.2 修改学生
		studentService.update(student);
		//6.3 构造R对象
		R r = R.ok().message("修改学生成功！");
		//6.4 转换为json并输出到前端
		String s = JSON.toJSONString(r);
		resp.getWriter().println(s);
		resp.getWriter().flush();


	}

	//7. 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		//7.1 得到要删除的学生编号
		String sid = req.getParameter("sid");
		//7.2 删除学生
		studentService.delete(sid);
		//7.3 构造R对象
		R r = R.ok().message("删除学生成功！");
		//7.4 转换为json并输出到前端
		String s = JSON.toJSONString(r);
		resp.getWriter().println(s);
		resp.getWriter().flush();


	}
}
